package com.study.juc.lk;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: juc-study
 * @author: Zhoujian
 * @date: 2022-08-12 20:55
 * @version: 1.0
 * @description: 卖票案例
 **/
public class SaleTicketDemo {
	public static void main(String[] args)
	{
		Ticket ticket = new Ticket();

		new Thread(() -> { for (int i = 0; i <35; i++)  ticket.sale(); },"a").start();
		new Thread(() -> { for (int i = 0; i <35; i++)  ticket.sale(); },"b").start();
		new Thread(() -> { for (int i = 0; i <35; i++)  ticket.sale(); },"c").start();
	}
}


class Ticket
{
	private int number = 50;
	ReentrantLock lock = new ReentrantLock();

	public void sale()
	{
		lock.lock();
		try
		{
			if(number > 0)
			{
				System.out.println(Thread.currentThread().getName()+" 卖出第：\t"+(number--)+"\t 还剩下:"+number);
			}
		}finally {
			lock.unlock();
		}
	}
}